package com.example.hehecat.yuejian1.ui.fragment;


import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.MyPictureContract;
import com.example.hehecat.yuejian1.mvp.model.bean.PictureBean;
import com.example.hehecat.yuejian1.mvp.presenter.MyPicturePresenter;
import com.example.hehecat.yuejian1.ui.adapter.ItemDragAdapter;
import com.example.hehecat.yuejian1.ui.adapter.MyPictureAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.content.ContentValues.TAG;

public class MyPictureFragment extends Fragment implements MyPictureContract.View {

    Unbinder unbinder;
    int user_id;
    @BindView(R.id.my_picture1)
    RecyclerView myPicture1;


//    private MyPictureAdapter adapter;
    private ItemDragAdapter mAdapter;
    private MyPicturePresenter presenter;


    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_picture, container, false);
        unbinder = ButterKnife.bind(this, view);
        user_id = MyStaticVariable.newInstance().getUserid();


        //创建布局管理
        final RecyclerView recyclerView = view.findViewById(R.id.my_picture1);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MyItemDecoration());

        //创建适配器
//        adapter = new MyPictureAdapter(R.layout.item_list, getContext());
        mAdapter = new ItemDragAdapter(R.layout.item_list,null,getContext());

        presenter = new MyPicturePresenter(this);
        presenter.updateAdapterData(user_id);

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(myPicture1);

        //监听item点击
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter madapter, View view, int position) {
                moveToPictureInfo(Objects.requireNonNull(mAdapter.getItem(position)).getPictureid());
                Log.d(TAG, "onItemClick: " + mAdapter.getItem(position).getPicturename());
            }
        });

        OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
                presenter.deleteMyPicture(user_id,mAdapter.getItem(pos).getPictureid());
            }
            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.d(TAG, "clearView: "+pos);

            }
            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                Log.d(TAG, "onItemSwiped: "+pos);
//                presenter.deleteMyPicture(user_id,mAdapter.getItem(pos).getPictureid());
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {

            }
        };


        //给RecycleView设置适配器

        mAdapter.enableSwipeItem();
        mAdapter.setOnItemSwipeListener(onItemSwipeListener);
        recyclerView.setAdapter(mAdapter);
        return view;
    }

    public static MyPictureFragment newInstance(String name) {
        MyPictureFragment fragment = new MyPictureFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void updateAdapterData(List<PictureBean> data) {
//        adapter.addData(data);
        mAdapter.addData(data);
    }


    @Override
    public void moveToPictureInfo(int picture_id) {
        PictureInfoFragment pictureInfoFragment = PictureInfoFragment.newInstance(picture_id);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, pictureInfoFragment, null)
                .commit();
    }

    @OnClick(R.id.foodinfo_return)
    void pictureCollectReturn(){
        MineFragment fragment = new MineFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content,fragment,null)
                .commit();
    }

}
